/*
*	mgFargo.e - Translate Magic PC v8.3 through v5.70 exported doc files.
*	Version 23
*
*	This source file is the source of a compiled macro that runs under
*	the Epsilon (see note) text editor.
*
*	Incorporating the macro into Epsilon:
*	-------------------------------------
*	1. Read this file into Epsilon (load it like any other ordinary file)
*	2. Run the command 'compile-buffer'.
*	3. Run the command 'write-state' so that the compiled buffer is made part
*		of your epsilon.sta file and will be automatically available next
*		time epsilon loads.
*
*   Note: "Epsilon" is a registered trademark licensed to Lugaru Software, Ltd. 
*	"EEL" and "Lugaru" are trademarks of Lugaru Software, Ltd.	
*
*	Using the macros:
*	----------------
*	 new_ezr_when_idle						Same as when_idle, somehow
*	 when_idle								called from Epsilon when its idle so I can display mgFargo related stuff on status line
*	 note_current_task()					Used by when_idle to display the current task.
*	 get_real_virt_from_range_locate()		??
*	 magic_unnumber_virtual_fields()		??
*	 magic_to_vb()							Feeble unfinished attempt to convert Magic to Visual Basic
*	
*
*	Created:
*		2006-0117 Ira Gershenhorn 
*	
*	Updated:
*		2006-0117 IG moved from dev\epsilon to \dev\mgFargo.  Removed standalone Translation functions
*		08/04/03 IG V23 for Epsilon Version 12.  mode-start and	mode-end replaced by new variable mode-format.
*       08/13/98 IG V22 replace all percent symbols (epsilon escape) to avoid ftp open file crash.
*		06/18/98 IG V21 Replace any redirection symbols with hyphens in program name so a legit filename can be written.
*		06/18/98 IG V20 Update the idle subroutine for Epsilon 9.
*		06/23/97 IG V19 Add support of Last Update with new doc_std.ira
*		06/20/97 IG V18 Fix bugs in writing to files. Eliminate asterisks, question marks and colons from file names.
*		06/17/96 IG Add task range and end task expressions.        
*		            Add select range and locate expressions.
*		            Add Link statemend.
*		06/19/96 IG Speed ups
*		06/19/96 IG Full support for In/Output Forms
*		06/20/96 IG Replaced fill_paragraph w/replace "\n and 5 spaces"
*		06/30/96 IG Fix exit on expression
					Add comments
		07/01/96 IG Add idle time task id.
		07/02/96 IG Buffer name is derived from original filename by tacking
					 on '.ezr'
		01/20/97 IG Date stamp the first task with the run date.
*		05/08/97 IG Add ability in NT to take filename from program name if extension is not 'm'
*        
*/
#include "eel.h"
int firstTask=1;
struct time_info ts;
int save_undo_size;
int suppress_dings;
int VARsExist;
int savepoint, from, to, endpoint, expressionsPoint, exptablePoint, outmark, brkLevelTablePoint;
int IOFilesPoint, DBFilesPoint;
int lineStart;
char spinString[]="|/-\\|/-\\";
int sizeExtension;
char progName[100];
char progNumber[3+1];

int formsPoint[200];
int formMark;
int ioPoint[50];
int ioExp[50];

char d[300];
char d2[300];
char operationsString[100];

char ss[20]; //searchstring
char outputBufName[40];
char taskName[40];
int	outputBufNum;
char mainBufName[40];
int mainBufNum;
int deriveFnFromPrgName;
   
int isParameter;
int indentLevel;
int nestedIndentLevel;
int indentLevelPrev;
char indentExp[255];
int	wantBlockClose = 0;
char emulation = 'B'; //B = Visual Basic; C = C/C++
int done;

int	save_case_fold;
int	save_margin_right;

jmp_buf quick_exit;

#define SPACE 0x20


//#define DEBUG
//#define ShowRefresh


zeroed TIMER idle_display_time;
#define EPS12 1
#define NEW_WHEN_IDLE  1
#ifdef NEW_WHEN_IDLE
// version 8 or greater
//int new_ezr_when_idle(int cnt)    /* will be renamed to when_idle */
int new_ezr_when_idle(int cnt)    /* will be renamed to when_idle */
{
	int mode_format_i = find_index("mode_format");

	if ( !cmd_len && !cnt && !_doing_input && !_doing_completion ){  //
		if( 0 != strstr( filename, ".ezr")||
			0 != strstr( bufname, ".ezr" ) 
		  )
			note_current_task();
		else {
#ifdef EPS12
			set_mode_message( " " );
			strcpy ( varptr( mode_format_i),  " %b%E%M %d%p %s%f" );
#else
		strcpy( mode_start, "" );
#endif
		}
		make_mode();
	}
	return ezr_when_idle(cnt); /* call old (which will have this name) */
}
REPLACE_FUNC("ezr", "when_idle")

#else
//for version 7 
when_idle(cnt)		// called when there's nothing to do, waiting for key
{
	int mode_format_i = find_index("mode-format");

	when_idle_coloring(cnt);
	if (cnt > 0 && !time_done(&idle_display_time))
		return( 1000 );//fix suggested by Steve Doerfler
	if (!_doing_input && !_doing_completion  && !cmd_len){
		if( 0 != strstr( filename, ".ezr")||
			0 != strstr( bufname, ".ezr" ) 
		  )
			note_current_task();
		else {
#ifdef EPS12
			set_mode_message( " " );
			strcpy ( varptr( mode_format_i),  " %b%E%M %d%p %s%f" );
#else
			strcpy( mode_start, "" );
#endif
		}
		make_mode();
	}
	time_begin(&idle_display_time, 1000);// obsolete? 100 = 10 times a second
	return( 1000  ); //says don't call here for until a second (1000 milliseconds) has elapsed
					// without this return, CPU utilization pegs 100%.
}
#endif
replace_chars( s, chars )
char *s, *chars;
{
	int len, i;
	char *d;

	len = strlen( chars );
	for( i=0; i<len; i++ ){
		d = s;
		while( d = index(d, chars[i]))
			d[0] = '_';
	}
}
/*command replace_in_region()
{
	grab( mark, point, d);
	replace_chars( d, "(){}" );
	stuff( d );
}
*/
command note_current_task()
{
	int savepoint = point;
	int sp;
	char dd[100];
	int mode_format_i = find_index("mode-format");

	if( re_search( -1, "^Task Name: " )){
		sp = matchstart;
		re_search( 1, "Last Update|\n" );
		//grab( sp, point-1, dd );
		grab( sp, matchstart-1, dd );
		if(strlen( dd) >27 )
			dd[27] = '\0';
		replace_chars( dd, "()%" );
		strcat( dd, " " );
	}
	else
		strcpy( dd, "" );
	
#ifdef EPS12
	// use Extra Text in place of current function because Epsilon can't figure out the current function.
	set_mode_message( dd );
	strcpy ( varptr( mode_format_i),  " %E %b%M %d%p %s%f %l %c" ); //extra text, Buffer File, buffer mode, display column, current function, current line, current column
//	say( " %d %s ", mode_format_i, get_str_var(mode_format_i));
#else
	strcpy( mode_start, dd );
	//strcat( mode_start, " " );
	strcpy(mode_end, " %d%p %s %l %c");
#endif
	point = savepoint;
}

/*
Matches up Range and Locate expressions in a grep buffer with the respective virtual or real fields.

Step 1:  Create a GREP buffer from the (Range|Locate)
Step 2:  ??
Step 3:  ??Run this macro??
*/
command get_real_virt_from_range_locate()
{
	char field[1000];
	char ref[100];
	char exp[900];
	int grepBufNum = window_bufnum;
	int outputBufNum, refBufNum;

	zap( "output" );
	outputBufNum = name_to_bufnum( "output" );
	while( re_search( 1, "(Range|Locate) (From|  To):" )){
		mark = matchstart;
		beginning_of_line();
		grab( point, mark, ref);
		end_of_line();
		grab( mark, point, exp);		
		grep_examine();	// jump to reference
		re_search( -1, "(Real|Virt):" );// backwards
		grab_full_line(window_bufnum, field);
		refBufNum = window_bufnum;
		window_bufnum = outputBufNum;
		buf_delete( refBufNum);
		stuff( ref );
		stuff( "\n" );
		stuff( field );
		beginning_of_line();
		delete_horizontal_space();
		stuff( "\t" );
		end_of_line();
		stuff( "\n\t\t" );
		stuff( exp );
		stuff( "\n" );
		window_bufnum = grepBufNum;
		end_of_line();
	}
}
/* Remove numbers from virtual fields so they can be compared better (ie. without failing each line due to position difference)*/
command magic_unnumber_virtual_fields()
{
	string_replace( "^ [ 0-9][ 0-9][0-9]", "", REGEX );
}


command magic_to_vb()
{
	string_replace( "	(Virt|Real):", "	", REGEX );	
	string_replace( "$", "USD", 0 );	
	string_replace( "\'", "\"", 0 );	
//	string_replace( "([A-Za-z])/([A-Za-z])", "#1_#2", REGEX );	
	string_replace( "//", "\'", 0 );	
	string_replace( " (AND|OR|NOT) ", "~#1~", REGEX );	
	string_replace( "	If ", "	If~", 0 );	
	string_replace( "(	IF~.*)", "#1~Then", REGEX );	
	string_replace( "([+*=]) IF", "#1 IIf", REGEX );
	string_replace( "([A-Za-z])[./ ]([_A-Za-z])", "#1_#2", REGEX );
	string_replace( " - ", "~~", 0);
	string_replace( "-", "_", 0 );
	string_replace( "~~"," - ", 0);
	string_replace( "~", " ", 0 );	
	string_replace( " (Today)", "_Today ", 0 );	
}
/*command x(){

	while( search( "^\t*" ){
	}
	string_replace( "([A-Za-z]) ([A-Za-z])", "#1_#2", REGEX );
};
*/
